package com.aw.guiguzi.module.manager.transaction

import android.view.View
import android.widget.RadioGroup
import com.aw.baselib.base.AwBaseBindingActivity
import com.aw.guiguzi.R
import com.aw.guiguzi.databinding.ActivityManagerTransactionSettingBinding
import com.aw.guiguzi.event.EventRefreshTransactionListType
import com.aw.guiguzi.listeners.OnCommonCheckedListener
import com.aw.guiguzi.util.AppCacheUtil
import com.aw.guiguzi.util.CommonUtil
import com.aw.guiguzi.util.CommonUtil.isAllow
import com.aw.guiguzi.util.CommonUtil.setOnCheckedChangeListenerNoAuth

class ManagerTransactionSettingActivity :
    AwBaseBindingActivity<ActivityManagerTransactionSettingBinding?>() {
    override fun getViewBinding(): ActivityManagerTransactionSettingBinding {
        return ActivityManagerTransactionSettingBinding.inflate(
            layoutInflater
        )
    }

    override fun initView() {
        super.initView()
        setToolbar("交易设置", null)
    }

    override fun initData() {
        super.initData()
        binding!!.switchConfigAdd.isChecked = AppCacheUtil.isTransactionConfigAdd()
        binding!!.switchConfigDel.isChecked = AppCacheUtil.isTransactionConfigDel()
        when (AppCacheUtil.getGridImgNums()) {
            2 -> binding!!.rBtnGridImg2.isChecked = true
            3 -> binding!!.rBtnGridImg3.isChecked = true
            4 -> binding!!.rBtnGridImg4.isChecked = true
        }
    }

    override fun initListener() {
        super.initListener()
        setOnCheckedChangeListenerNoAuth(
            binding!!.switchConfigAdd,
            object : OnCommonCheckedListener {
                override fun callback(isChecked: Boolean) {
                    if (isChecked) {
                        AppCacheUtil.setTransactionConfigAdd(true)
                        showMsg("交易配置动态新增已开启")
                    } else {
                        AppCacheUtil.setTransactionConfigAdd(false)
                        showMsg("交易配置动态新增已禁用")
                    }
                }

            })
        setOnCheckedChangeListenerNoAuth(
            binding!!.switchConfigDel,
            object : OnCommonCheckedListener {
                override fun callback(isChecked: Boolean) {
                    if (isChecked) {
                        AppCacheUtil.setTransactionConfigDel(true)
                        showMsg("交易配置动态删除已开启")
                    } else {
                        AppCacheUtil.setTransactionConfigDel(false)
                        showMsg("交易配置动态删除已禁用")
                    }
                }
            })
        binding!!.tvToConfig.setOnClickListener { v: View? ->
            toClass(
                ManagerTransactionConfigActivity::class.java, false
            )
        }
        binding!!.rgGridImg.setOnCheckedChangeListener { group: RadioGroup?, checkedId: Int ->
            if (!isAllow) {
                return@setOnCheckedChangeListener
            }
            when (checkedId) {
                R.id.rBtn_gridImg2 -> AppCacheUtil.setGridImgNums(2)
                R.id.rBtn_gridImg3 -> AppCacheUtil.setGridImgNums(3)
                R.id.rBtn_gridImg4 -> AppCacheUtil.setGridImgNums(4)
            }
            postEvent(EventRefreshTransactionListType(null))
        }
    }
}